<template>
  <div class="content">
    <THeader :active="this.$route.path"></THeader>

    <el-row style="padding-top: 100px">
      <el-col :span="10" :offset="7">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <h2 style="margin: 0">用户注册</h2>
          </div>
          <el-form :model="registerForm" label-width="100px" :rules="rules" ref="ruleForm" style="padding-right: 80px">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerForm.username"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="registerForm.email"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="registerForm.password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="qpassword">
              <el-input v-model="registerForm.qpassword"></el-input>
            </el-form-item>
          </el-form>
          <div style="padding-top: 20px;text-align: center">
            <el-button style="width: 120px" size="large" type="primary" @click="registerAction">注册</el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!--联系方式-->
  </div>
</template>

<script>
import { registerServer } from "../../api/user";

export default {
  name: "register",
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      registerForm: {
        username: "",
        email: "",
        password: "",
        qpassword: ""
      },
      rules: {
        username: [
          { required: true, message: "用户名不能少", trigger: "blur" },
          { min: 6, max: 16, message: "用户名在6到16位之间", trigger: "blur" }
        ],
        email: [
          { required: true, message: "用户名不能少", trigger: "blur" },
          { min: 6, max: 16, message: "用户名在6到16位之间", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        qpassword: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  methods: {
    registerAction() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          const { username, email, password, qpassword } = this.registerForm;
          registerServer("post", { username, email, password, qpassword }).then(
            suc => {
              const { code, message } = suc.data;
              if (code) {
                this.$router.push("/login");
                this.$message({
                  type: "success",
                  message
                });
              } else {
                this.$message({
                  type: "error",
                  message
                });
              }
            },
            err => {
              this.$message({
                type: "error",
                message: err.message
              });
            }
          );
        }
      });
    }
  }
};
</script>

<style lang="less">
.register_area {
  margin: 50px auto 60px;
  width: 400px;
  h2 {
    height: 34px;
    line-height: 34px;
    font-size: 24px;
    color: #f30;
    margin-bottom: 34px;
  }
}

.register {
  label {
    width: 100px;
    text-align: right;
    font-size: 14px;
    color: #666;
    display: inline-block;
    padding-right: 10px;
  }
  abbr {
    color: #f30;
    font-style: none;
    margin-right: 5px;
  }
  p {
    margin-bottom: 18px;
    position: relative;
  }
}

.button-rounded {
  border: 1px solid #0381aa;
  color: #fff;
  background: #337ccb;
  cursor: pointer;
  margin-left: 25px;
  border-radius: 2px;
  width: 340px;
  height: 35px;
  line-height: normal;
  font-size: 16px;
}

.input-rounded {
  width: 238px;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  border: 1px solid #ccc;
  font-size: 15px;
  color: #333;
  vertical-align: middle;
}
</style>
